import sys
from PySide import QtGui, QtCore
import linuxcnc

class ManualWidget(QtGui.QWidget):
	def __init__(self, mainApp, parent=None):
		QtGui.QWidget.__init__(self, parent)
		
		self.mainApp = mainApp
		self.emcStat = self.mainApp.getEmcStat()
		
		self.jogModes = ["continious", "1.00", "0.1", "0.01"]
		self.jogMode = "continious"
		
		self.MAX_VELOCITY = 500.0 #self.emcStat.max_velocity
		self.velocity = self.MAX_VELOCITY/2.0
				
		self.initGui()

	def initGui(self):
		self.label1 = QtGui.QLabel("Jog mode:", self)
		self.comboBox = QtGui.QComboBox(self)
		self.comboBox.addItems(self.jogModes)
		self.comboBox.currentIndexChanged.connect(self.jogModeChanged)
		
		self.label2 = QtGui.QLabel("Jog speed:", self)
		self.slider = QtGui.QSlider(QtCore.Qt.Orientation.Horizontal)
		self.slider.setRange(0.0, 1000.0)
		self.slider.setTickPosition(QtGui.QSlider.TicksBelow)
		self.slider.setValue(self.velocity)
		self.slider.valueChanged.connect(self.jogSpeedChanged)
		
		self.speedLcd = QtGui.QLCDNumber(4, self)
		self.speedLcd.display(self.velocity)
		
		
		#jog buttons
		self.btnxPlus = QtGui.QPushButton("", self)
		self.btnxPlus.setFixedSize(64, 64)
		self.btnxPlus.setIconSize(QtCore.QSize(48, 48))
		self.btnxPlus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/xplus.svg"))
		self.btnxPlus.pressed.connect(self.jogAxis)
		self.btnxPlus.released.connect(self.stopjogAxis)
		
		self.btnxMinus = QtGui.QPushButton("", self)
		self.btnxMinus.setFixedSize(64, 64)
		self.btnxMinus.setIconSize(QtCore.QSize(48, 48))
		self.btnxMinus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/xmin.svg"))
		self.btnxMinus.pressed.connect(self.jogAxis)
		self.btnxMinus.released.connect(self.stopjogAxis)
		
		self.btnyPlus = QtGui.QPushButton("", self)
		self.btnyPlus.setFixedSize(64, 64)
		self.btnyPlus.setIconSize(QtCore.QSize(48, 48))
		self.btnyPlus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/yup.svg"))
		self.btnyPlus.pressed.connect(self.jogAxis)
		self.btnyPlus.released.connect(self.stopjogAxis)
		
		self.btnyMinus = QtGui.QPushButton("", self)
		self.btnyMinus.setFixedSize(64, 64)
		self.btnyMinus.setIconSize(QtCore.QSize(48, 48))
		self.btnyMinus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/ydown.svg"))
		self.btnyMinus.pressed.connect(self.jogAxis)
		self.btnyMinus.released.connect(self.stopjogAxis)
		
		self.btnzPlus = QtGui.QPushButton("", self)
		self.btnzPlus.setFixedSize(64, 64)
		self.btnzPlus.setIconSize(QtCore.QSize(48, 48))
		self.btnzPlus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/zup.svg"))
		self.btnzPlus.pressed.connect(self.jogAxis)
		self.btnzPlus.released.connect(self.stopjogAxis)
		
		self.btnzMinus = QtGui.QPushButton("", self)
		self.btnzMinus.setFixedSize(64, 64)
		self.btnzMinus.setIconSize(QtCore.QSize(48, 48))
		self.btnzMinus.setIcon(QtGui.QIcon("/home/andris/aScreen/resources/zdown.svg"))
		self.btnzMinus.pressed.connect(self.jogAxis)
		self.btnzMinus.released.connect(self.stopjogAxis)
		 
		self.grid = QtGui.QGridLayout()
		
		self.grid.addWidget(self.label1, 1, 0)
		self.grid.addWidget(self.comboBox, 2, 0, 1, 3)
		
		self.grid.addWidget(self.label2, 3, 0)
		self.grid.addWidget(self.slider, 4, 0, 1, 3)
		self.grid.addWidget(self.speedLcd, 5, 0, 1, 2)
		
		self.grid.addWidget(self.btnxPlus, 7, 2)
		self.grid.addWidget(self.btnxMinus, 7, 0)
		self.grid.addWidget(self.btnyPlus, 6, 1)
		self.grid.addWidget(self.btnyMinus, 7, 1)
		self.grid.addWidget(self.btnzPlus, 5, 2)
		self.grid.addWidget(self.btnzMinus, 6, 2)
		self.setLayout(self.grid)

	def jogAxis(self):
		sender = self.sender()
		jogAxis = None
		direction = None
		
		if sender == self.btnxPlus:
			jogAxis = 0
			direction = 1
		elif sender ==  self.btnxMinus:
			jogAxis = 0
			direction = -1
		elif sender == self.btnyPlus:
			jogAxis = 1
			direction = 1
		elif sender == self.btnyMinus:
			jogAxis = 1
			direction = -1
		elif sender == self.btnzPlus:
			jogAxis = 2
			direction = 1
		elif sender == self.btnzMinus:
			jogAxis = 2
			direction = -1
			
		if jogAxis != None:
			self.mainApp.jogAxis(self.jogMode, jogAxis, direction, self.velocity)
			
	def stopjogAxis(self):
		sender = self.sender()
		if self.jogMode == "continious":
			if sender == self.btnxPlus or sender == self.btnxMinus:
				self.mainApp.stopJog(0)
			elif sender == self.btnyPlus or sender == self.btnyMinus:
				self.mainApp.stopJog(1)
			elif sender == self.btnzPlus or sender == self.btnzMinus:
				self.mainApp.stopJog(2)
			
	def jogSpeedChanged(self):
		self.velocity = self.slider.value()
		self.speedLcd.display(self.velocity)
		
	def jogModeChanged(self):
		self.jogMode = self.comboBox.currentText()
		
	def updateData(self):
		pass
		
if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
    	window = ManualWidget()
    	window.show()
    	sys.exit(app.exec_())
